package com.jianzong.lingxuan.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jianzong.lingxuan.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @auth Leiwenlong
 * @since 2021/7/26
 *   分页逻辑处理类
 */
@Component
@Service
public class PageUtil {

    public static class CheckToPage {
        private boolean myResult;
        private Map<String, String[]> map;
        private String token;
        private Object id;
        private Object userPetsId;
        private Object userMountId;
        private Object twoUsertId;
        private Object friendName;
        private Object relationship;
        private Object applicantContext;
        private Page<Map<String, Object>> page;
        private RedisService redisService;

        public CheckToPage(Map<String, String[]> map, String token,RedisService redisService) {
            this.map = map;
            this.token = token;
            this.redisService = redisService;
        }

        public boolean is() {
            return myResult;
        }

        public Object getId() {
            return id;
        }

        public Object getUserPetsId() {
            return userPetsId;
        }

        public Object getUserMountId() {
            return userMountId;
        }

        public Object getTwoUserId() { return twoUsertId;}

        public Object getFriendName() {return friendName;}

        public Object getRelationship() {return relationship;}

        public Object getApplicantContext() {return applicantContext;}

        public Page<Map<String, Object>> getPage() {
            return page;
        }

        public CheckToPage invoke() {
            //获取信息
            Object tokens = redisService.get(token);

            if(ObjectUtil.isNull(tokens) || ObjectUtil.isEmpty(tokens) ){
                myResult = true;
                return this;
            }

            Map<String, Object> objectMap = (Map<String, Object>) tokens;
            id = objectMap.get("id");

            if(ObjectUtil.isNotNull(map.get("userPetsId"))){
                userPetsId = map.get("userPetsId")[0];
            }

            if(ObjectUtil.isNotNull(map.get("userMountId"))){
                userMountId = map.get("userMountId")[0];
            }

            if(ObjectUtil.isNotNull(map.get("twoUserId"))){
                twoUsertId = map.get("twoUserId")[0];
            }

            if(ObjectUtil.isNotNull(map.get("friendName"))){
                friendName = map.get("friendName")[0];
            }

            if(ObjectUtil.isNotNull(map.get("relationship"))){
                relationship = map.get("relationship")[0];
            }

            if(ObjectUtil.isNotNull(map.get("applicantContext"))){
                applicantContext = map.get("applicantContext")[0];
            }

            String current = "1";
            String size = "5";

            if(ObjectUtil.isNotNull(map.get("current"))){
                current = map.get("current")[0];
            }

            if(ObjectUtil.isNotNull(map.get("size"))){
                size = map.get("size")[0];
            }

            page = new Page<Map<String, Object>>();
            page.setSize(Integer.parseInt(size));
            page.setCurrent(Integer.parseInt(current));
            myResult = false;
            return this;
        }

    }
}
